//connexion au site
function connexion(){   
    var login = document.getElementsByName("connexionlogin1")[0].value;
    var mdp = document.getElementsByName("connexionMdp1")[0].value;
    if(login == "" || mdp == ""){
        $(".message1").html("tous les champs doivent etre remplis");
    }    
    else if(login == "admin" && mdp == "passe"){
        //verification mot de passe en dure : administrateur du site
         var addr = "ServletAction?action=connexion&login=admin&admin=true";
         window.location.replace(addr);
         
    }
    else{
        //verification pour un utilisateur lambda
         $.post("ServletAction","action=connexionUser&login="+login+"&mdp="+mdp, function(data) {
                if(data=="ok"){
                    window.location.replace("ServletAction?action=connexion&login="+login+"&admin=false");
                }else{
                    document.getElementsByName("connexionMdp1")[0].value = "";
                     $(".message1").html("le login et/ou le mot de passe est incorrect");
                }
         });
    }
}

//inscription
function inscription(){
    var login = document.getElementsByName("connexionlogin2")[0].value;
    var mdp = document.getElementsByName("connexionMdp2")[0].value;
    var mdpbis = document.getElementsByName("connexionMdp2bis")[0].value;
    if(mdp=="" || login=="" || mdpbis==""){
        $(".message2").html("tous les champs doivent etre remplis");
    }
    else if(mdp != mdpbis){
        document.getElementsByName("connexionMdp2")[0].value = "";
        document.getElementsByName("connexionMdp2bis")[0].value = "";
        $(".message2").html("les deux mots de passe sont differents");
    }
    else{
         $.post("ServletAction", "action=inscription&login="+login+"&mdp="+mdp,function(data) {
             $(".message3").html("Vous etes inscrit, vous pouvez vous connecter");
             $(".message2").html("");
             document.getElementsByName("connexionlogin2")[0].value = "";
             document.getElementsByName("connexionMdp2")[0].value = "";
             document.getElementsByName("connexionMdp2bis")[0].value = "";
         });
    }
}

//ajout du jeux dans le panier
function addToPanier(id){
    $.post("ServletAction","action=addToPanier&id="+id, function(data) {
       $("span.pricePanier").html(data.split(";")[1] + " &#8364;");
       $("span.nbPanier").html(data.split(";")[0]);
    });
}

//page detail -> vote
function voter(){
    $(".voter").slideToggle("slow");
}

function notea(){
    document.getElementsByName("note1")[0].src = "images/favs.gif";
    document.getElementsByName("note2")[0].src = "images/favs.png";
    document.getElementsByName("note3")[0].src = "images/favs.png";
    document.getElementsByName("note4")[0].src = "images/favs.png";
    document.getElementsByName("note5")[0].src = "images/favs.png";
}
function noteb(){
    document.getElementsByName("note1")[0].src = "images/favs.gif";
    document.getElementsByName("note2")[0].src = "images/favs.gif";
    document.getElementsByName("note3")[0].src = "images/favs.png";
    document.getElementsByName("note4")[0].src = "images/favs.png";
    document.getElementsByName("note5")[0].src = "images/favs.png";
}
function notec(){
    document.getElementsByName("note1")[0].src = "images/favs.gif";
    document.getElementsByName("note2")[0].src = "images/favs.gif";
    document.getElementsByName("note3")[0].src = "images/favs.gif";
    document.getElementsByName("note4")[0].src = "images/favs.png";
    document.getElementsByName("note5")[0].src = "images/favs.png";
}
function noted(){
    document.getElementsByName("note1")[0].src = "images/favs.gif";
    document.getElementsByName("note2")[0].src = "images/favs.gif";
    document.getElementsByName("note3")[0].src = "images/favs.gif";
    document.getElementsByName("note4")[0].src = "images/favs.gif";
    document.getElementsByName("note5")[0].src = "images/favs.png";
}
function notee(){
    document.getElementsByName("note1")[0].src = "images/favs.gif";
    document.getElementsByName("note2")[0].src = "images/favs.gif";
    document.getElementsByName("note3")[0].src = "images/favs.gif";
    document.getElementsByName("note4")[0].src = "images/favs.gif";
    document.getElementsByName("note5")[0].src = "images/favs.gif";
}
function noteout(){
    document.getElementsByName("note1")[0].src = "images/favs.png";
    document.getElementsByName("note2")[0].src = "images/favs.png";
    document.getElementsByName("note3")[0].src = "images/favs.png";
    document.getElementsByName("note4")[0].src = "images/favs.png";
    document.getElementsByName("note5")[0].src = "images/favs.png";
}
function valider(note, id){
    $.post("ServletAction","action=voter&id="+id+"&note="+note, function(data) {
       var note = parseFloat(data);
       var refresh = "";
       if(note>0.0 && note <=0.5){
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
       }
       else if(note>0.5 && note<=1.5){
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
       }
       else if(note>1.5 && note<=2.5){
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
       }
       else if(note>2.5 && note<=3.5){
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
       }
       else if(note>3.5 && note<=4.5){
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.png' alt='' title='' border='0' class='left_bt' />";
       }
       else if(note>4.5 && note<=5){
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
           refresh+="<img src='images/favs.gif' alt='' title='' border='0' class='left_bt' />";
       }
       refresh += "<br/><br/><span class='blue'>vote valide !</span>";
       $("td.tdnote").html(refresh);
    });
}

function supprimerUser(){
    var liste = "";
    var first = true;
    var valeur = document.getElementsByName("supp");
      for (var i=0 ; i<valeur.length ; i++){
            if (valeur[i].checked){
                if(first){
                    first = false;
                    liste += valeur[i].value;
                }
                else{
                    liste += "_"+valeur[i].value;
                }
            }
        }
    
     if(liste != ""){
        window.location.replace("ServletAction?action=SupprimerUtilisateurList&liste="+liste);
     }       
}

//autocompletion sur la recherche a droite
function listResult(){
    $.ajax({url: 'ServletAction?action=rechercheCompletion',success: function(data) {
           if(data != 'vide'){
               var elementsArray = data.split(";");
               $("input#autocomplete").autocomplete({
                    source: elementsArray
                });
           }
    }
   });
}

function listeTrie(order){
    window.location.replace("ServletAction?action=allProduits&orderBy="+order);
}